# Live filesystem mounting			-*- shell-script -*-
# 

# Parameter: Where to mount the filesystem
mountroot ()
{
	# checkings
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-top"
	run_scripts /scripts/live-top
	[ "$quiet" != "y" ] && log_end_msg

	# Get the root filesystem type
	if [ ! -e "${ROOT}" ]; then
		panic "ALERT!  ${ROOT} does not exist.  Dropping to a shell!"
	fi

	# Find and mount the Image
	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-premount"
	run_scripts /scripts/live-premount
        if [ ! -e /mnt/image/etc ]; then
                panic "Something wrong with the image mounting"
                # Just in case
                FS1='(initramfs) ' exec /bin/sh </dev/console >/dev/console 2>&1
        fi
	[ "$quiet" != "y" ] && log_end_msg

	modprobe -q unionfs

	# Mount root
	unionfs=${rootmnt}
	changes=/mnt/changes
	mkdir -p ${changes}
	if [ -n ${dirconf} ] && [ "x${dirconf}" != "xram" ]; then
	changesdir=/mnt/changes/${dirconf}
	mkdir -p ${changesdir}
	fi
	image=/mnt/image
	dirs=${unionfs}/.dirs
	if [ -n ${dirconf} ] && [ "x${dirconf}" != "xram" ]; then
	mount -t unionfs -o dirs=${changesdir}=rw:${image}=ro unionfs ${unionfs} 
	else
	mount -t unionfs -o dirs=${changes}=rw:${image}=ro unionfs ${unionfs} 
	fi
	mkdir -p ${dirs}
	mount -o move /mnt ${dirs}
	
	if [ $? != 0 ]; then
		panic "Something wrong with the unionfs mounting"
		# Just in case
		FS1='(initramfs) ' exec /bin/sh </dev/console >/dev/console 2>&1
	fi

	[ "$quiet" != "y" ] && log_begin_msg "Running /scripts/live-bottom"
	run_scripts /scripts/live-bottom
	[ "$quiet" != "y" ] && log_end_msg
}
